package com.pfizer.pfizerExpertForPad;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * MainActivity--<b>主菜單介面</b>--
 * 
 * @author krui
 * @version 1.0 2011/11/22
 */
public class MainActivity extends Activity implements OnClickListener {

	Context mContext = MainActivity.this;
	ProgressDialog progressDialog=null;
	private final Timer timer = new Timer();
	private TimerTask task;
	private Thread thdMain;
	Handler handler = new Handler() {
	   public void handleMessage(Message msg) {
		   // TODO Auto-generated method stub
		   	 task.cancel();
		   	 if(progressDialog!=null)
		   	 {
		   		 progressDialog.dismiss();
		   	 }
		   	 thdMain.stop();
	   }
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenu);
		View v1 = findViewById(R.id.icon01);
		v1.setOnClickListener(this);
		View v2 = findViewById(R.id.icon02);
		v2.setOnClickListener(this);
		View v3 = findViewById(R.id.icon03);
		v3.setOnClickListener(this);
		View v4 = findViewById(R.id.icon04);
		v4.setOnClickListener(this);
		View btnInputCode = findViewById(R.id.btnInputCode);
		btnInputCode.setOnClickListener(this);
		View v5 = findViewById(R.id.btnDas);
		v5.setOnClickListener(this);

		View v6 = findViewById(R.id.btnSurvey);
		v6.setOnClickListener(this);
		 
	}

	/**
	 * 对imageview和button控件設置監聽事件
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		progressDialog=ProgressDialog.show(mContext, null, "Loading...", true);
		SystemClock.sleep(200);
		int Id = v.getId();
		runMain(Id);
		/*
		try {
			Intent intent = null;
			switch (Id) {
			case R.id.icon01:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "1");
				intent.putExtra("RecID", 1);
				startActivity(intent);
				break;
			case R.id.icon02:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "2");
				intent.putExtra("RecID", 1);
				startActivity(intent);
				break;
			case R.id.icon03:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "3");
				intent.putExtra("RecID", 1);
				startActivity(intent);
				break;
			case R.id.icon04:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "4");
				intent.putExtra("RecID", 1);
				startActivity(intent);
				break;
			case R.id.btnInputCode:
				intent = new Intent(mContext, DiseaseDialog.class);
				startActivity(intent);
				break;
			case R.id.btnDas:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "1");
				intent.putExtra("RecID", 2);
				startActivity(intent);
				break;
			case R.id.btnSurvey:
				intent = new Intent(mContext, DiseaseActivity.class);
				intent.putExtra("ClassID", "1");
				intent.putExtra("RecID", 4);
				startActivity(intent);
				break;
			default:
				break;
			}
			Message message = new Message();
        	handler.sendMessage(message);
		} catch (Exception e) {
			
		}
		*/
		//progressDialog.dismiss();
		task = new TimerTask() {
            @Override
            public void run() {
            	// TODO Auto-generated method stub
            	Message message = new Message();
            	handler.sendMessage(message);
            }
        };	        
		timer.schedule(task, 4500);
	}
	private void runMain(final int ID)
	{
		thdMain=new Thread(new Runnable(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					Intent intent = null;
					switch (ID) {
					case R.id.icon01:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "1");
						intent.putExtra("RecID", 1);
						startActivity(intent);
						break;
					case R.id.icon02:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "2");
						intent.putExtra("RecID", 1);
						startActivity(intent);
						break;
					case R.id.icon03:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "3");
						intent.putExtra("RecID", 1);
						startActivity(intent);
						break;
					case R.id.icon04:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "4");
						intent.putExtra("RecID", 1);
						startActivity(intent);
						break;
					case R.id.btnInputCode:
						intent = new Intent(mContext, DiseaseDialog.class);
						startActivity(intent);
						break;
					case R.id.btnDas:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "1");
						intent.putExtra("RecID", 2);
						startActivity(intent);
						break;
					case R.id.btnSurvey:
						intent = new Intent(mContext, DiseaseActivity.class);
						intent.putExtra("ClassID", "1");
						intent.putExtra("RecID", 4);
						startActivity(intent);
						break;
					default:
						break;
					}
					Message message = new Message();
	            	handler.sendMessage(message);
				} catch (Exception e) {
				}
			}});
		thdMain.start();
		
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mContext = null;
		progressDialog = null;
	}

}
